﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using Team6_Algorithms.Converter;
using Team6_Algorithms.AidingClasses;

/*
 *  Template Matching algoritm
 *  Author: Marleen Schilt, helped by Dylan Snel
 */


namespace Team6_Algorithms.HighEnd
{
    public class TemplateMatcher
    {
        private static Bitmap startBitmap;
        private static Bitmap templateBitmap;
        private static Bitmap finishedBitmap;

        private static double SAD, bestSAD; //(Sum of Absolute Distance)
        private static double minSAD;
        private static int maxVal, position, templateRows, templateCols, bestRow, bestCol;

        static PixelPoint[,] T;

        public static Bitmap matchTemplate(Bitmap sourceImage)
        {
            startBitmap = sourceImage;       
                    
            int y;
            int x;
            PixelPoint [,] S = (PixelPoint[,])Binary.Binaryze.InvertBinary(sourceImage);
            templateRows = templateBitmap.Height;
            templateCols = templateBitmap.Width;
            minSAD = templateCols * templateRows;      
            //Loop through the search image
            for (y = 0; y <= startBitmap.Height - templateRows; y++)
            {
                for (x = 0; x <= startBitmap.Width - templateCols; x++)
                {
                    SAD = 0.0;
                    //Loop through the template image
                    for (int i = 0; i < templateRows; i++)
                        for (int j = 0; j < templateCols; j++) {
                            //Bij wit en wit gebeurt er niks
                            //Bij wit en zwart is het -1, dus geen match
                            //Bij zwart en zwart is het +1, dus een match
                            
                            int pixelSearch = S[x + j, y + i].id;

                            int pixelTemplate = T[j, i].id;

                            if (pixelSearch == pixelTemplate)
                            {
                                
                            }
                            else
                            {
                                SAD += 1;
                            }
                        }
                }
                
                //Save the best found position (smallest SAD is best)
                if (minSAD > SAD) {
                    minSAD = SAD;
                    //Give me maxVal
                    bestRow = y;
                    bestCol = x;
                    bestSAD = SAD;
                }
            }

            finishedBitmap = sourceImage;

            Graphics gfx = Graphics.FromImage(finishedBitmap);
            gfx.DrawRectangle(Pens.Red, bestCol, bestRow, templateBitmap.Width, templateBitmap.Height);
            gfx.Dispose();

            return finishedBitmap;
        }

        public static void readTemplate(Bitmap templateImage)
        {
            templateBitmap = templateImage;

            T = (PixelPoint[,])Binary.Binaryze.InvertBinary(templateImage);
        }
    }
}
